import request from './request'

export interface ApiResponse<T = any> {
  code: number
  message: string
  data: T
}

export interface ProjectMember {
  id: number
  userId: number
  projectId: number
  account: string
  name: string
  deptName?: string
  phone?: string
  email?: string
  role: string
  createdAt: string
}

export interface AddProjectMembersRequest {
  projectId: number
  members: ProjectMember[]
}

export interface BatchAddProjectMembersRequest {
  projectId: number
  userIds: number[]
  role: string
}

export interface PrimaryKey {
  id: number
}

// 添加项目成员
export const addProjectMembers = (params: AddProjectMembersRequest): Promise<ApiResponse> => {
  return request.post('/projectMember/addMembers', params)
}

// 批量添加项目成员
export const batchAddProjectMembers = (params: BatchAddProjectMembersRequest): Promise<ApiResponse> => {
  return request.post('/projectMember/batchAddMembers', params)
}

// 删除项目成员
export const deleteProjectMember = (id: number): Promise<ApiResponse> => {
  return request.post('/projectMember/deleteMember', { id })
}

// 更新项目成员信息
export const updateProjectMember = (member: ProjectMember): Promise<ApiResponse> => {
  return request.post('/projectMember/updateMember', member)
}

// 获取项目成员列表
export const getProjectMembers = (projectId: number): Promise<ApiResponse> => {
  return request.post(`/projectMember/getMembers`, { id: projectId })
}

// 检查用户是否已是项目成员
export const checkMemberExists = (projectId: number, userId: number): Promise<ApiResponse<{ exists: boolean }>> => {
  return request.get(`/projectMember/checkMemberExists/${projectId}/${userId}`)
}
